<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>module SmartSearch::ClassMethods - SmartSearch</title>

<link type="text/css" media="screen" href="../rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
</script>

<script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="../js/darkfish.js"></script>


<body id="top" class="module">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="../index.html">Home</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  <div id="file-metadata">
    <nav id="file-list-section" class="section">
  <h3 class="section-header">Defined In</h3>
  <ul>
    <li>lib/smart_search.rb
  </ul>
</nav>

    
  </div>

  <div id="class-metadata">
    
    
    
    <!-- Method Quickref -->
<nav id="method-list-section" class="section">
  <h3 class="section-header">Methods</h3>

  <ul class="link-list">
    
    <li><a href="#method-i-find_by_tags">#find_by_tags</a>
    
    <li><a href="#method-i-is_smart_search-3F">#is_smart_search?</a>
    
    <li><a href="#method-i-result_template_path">#result_template_path</a>
    
    <li><a href="#method-i-set_search_index">#set_search_index</a>
    
    <li><a href="#method-i-set_similarity_index">#set_similarity_index</a>
    
    <li><a href="#method-i-smart_search">#smart_search</a>
    
  </ul>
</nav>

  </div>

  <div id="project-metadata">
    
    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="../SmartSearch.html">SmartSearch</a>
  
    <li><a href="../SmartSearch/ClassMethods.html">SmartSearch::ClassMethods</a>
  
    <li><a href="../SmartSearch/Config.html">SmartSearch::Config</a>
  
    <li><a href="../SmartSearch/InstanceMethods.html">SmartSearch::InstanceMethods</a>
  
    <li><a href="../SmartSearch/SmartSearchEngine.html">SmartSearch::SmartSearchEngine</a>
  
    <li><a href="../SmartSearchHistory.html">SmartSearchHistory</a>
  
    <li><a href="../SmartSearchIgnoreWord.html">SmartSearchIgnoreWord</a>
  
    <li><a href="../SmartSearchTag.html">SmartSearchTag</a>
  
    <li><a href="../SmartSimilarity.html">SmartSimilarity</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation">
  <h1 class="module">module SmartSearch::ClassMethods</h1>

  <div id="description" class="description">
    
<p>Class Methods for ActiveRecord</p>

  </div><!-- description -->

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    

    <!-- Methods -->
    
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Instance Methods</h3>

    
      <div id="method-i-find_by_tags" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">find_by_tags</span><span
            class="method-args">(tags = "", options = {})</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Serach database for given search tags</p>
          

          
          <div class="method-source-code" id="find_by_tags-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 69</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">find_by_tags</span>(<span class="ruby-identifier">tags</span> = <span class="ruby-string">&quot;&quot;</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">is_smart_search?</span>
    
    <span class="ruby-comment"># Save Data for similarity analysis</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">tags</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">3</span>
      <span class="ruby-keyword">self</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">execute</span>(<span class="ruby-node">&quot;INSERT INTO `#{::SmartSearchHistory.table_name}` (`query`) VALUES (&#39;#{tags.gsub(/[^a-zA-ZäöüÖÄÜß\ ]/, &#39;&#39;)}&#39;);&quot;</span>)
    <span class="ruby-keyword">end</span>  
    
    <span class="ruby-identifier">tags</span> = <span class="ruby-identifier">tags</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">&quot; &quot;</span>)
    
    <span class="ruby-comment"># Fallback for Empty String</span>
    <span class="ruby-identifier">tags</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;#&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">tags</span>.<span class="ruby-identifier">empty?</span>
    
    <span class="ruby-comment"># Similarity</span>
    <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">enable_similarity</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">true</span>
      <span class="ruby-identifier">tags</span>.<span class="ruby-identifier">map!</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span>   
        <span class="ruby-identifier">similars</span> = <span class="ruby-constant">SmartSimilarity</span>.<span class="ruby-identifier">similars</span>(<span class="ruby-identifier">t</span>, <span class="ruby-value">:increment_counter</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;|&quot;</span>)
        <span class="ruby-node">&quot;search_tags REGEXP &#39;#{similars}&#39;&quot;</span>
      <span class="ruby-keyword">end</span>  
      
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">tags</span>.<span class="ruby-identifier">map!</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span> <span class="ruby-node">&quot;search_tags LIKE &#39;%#{t}%&#39;&quot;</span>}
    <span class="ruby-keyword">end</span>  
    
    <span class="ruby-comment"># Load ranking from Search tags</span>
    <span class="ruby-identifier">result_ids</span> = <span class="ruby-constant">SmartSearchTag</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">select_all</span>(<span class="ruby-node">&quot;select entry_id, sum(boost), group_concat(search_tags) as grouped_tags 
    from smart_search_tags where `table_name`= &#39;#{self.table_name}&#39; and 
    
    (#{tags.join(&#39; OR &#39;)}) group by entry_id having (#{tags.join(&#39; AND &#39;).gsub(&#39;search_tags&#39;, &#39;grouped_tags&#39;)}) order by sum(boost) DESC&quot;</span>).<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span>[<span class="ruby-string">&quot;entry_id&quot;</span>]}
    
    <span class="ruby-identifier">results</span> =  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">where</span>(<span class="ruby-value">:id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">result_ids</span>)
    
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>]
      <span class="ruby-identifier">results</span> = <span class="ruby-identifier">results</span>.<span class="ruby-identifier">where</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>])
    <span class="ruby-keyword">end</span>
    
    <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-keyword">self</span>.<span class="ruby-identifier">condition_default</span>.<span class="ruby-identifier">blank?</span>
      <span class="ruby-identifier">results</span> = <span class="ruby-identifier">results</span>.<span class="ruby-identifier">where</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">condition_default</span>)
    <span class="ruby-keyword">end</span>  
    
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:group</span>]
      <span class="ruby-identifier">results</span> = <span class="ruby-identifier">results</span>.<span class="ruby-identifier">group</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:group</span>])
    <span class="ruby-keyword">end</span>  
    
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:order</span>]
      <span class="ruby-identifier">results</span> = <span class="ruby-identifier">results</span>.<span class="ruby-identifier">order</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:order</span>])
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">results</span> = <span class="ruby-identifier">results</span>.<span class="ruby-identifier">order</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">order_default</span>)
    <span class="ruby-keyword">end</span>    
            
    <span class="ruby-keyword">return</span> <span class="ruby-identifier">results</span>
  <span class="ruby-keyword">else</span>                      
    <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;#{self.inspect} is not a SmartSearch&quot;</span>
  <span class="ruby-keyword">end</span>  
<span class="ruby-keyword">end</span></pre>
          </div><!-- find_by_tags-source -->
          
        </div>

        

        
      </div><!-- find_by_tags-method -->

    
      <div id="method-i-is_smart_search-3F" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">is_smart_search?</span><span
            class="method-args">()</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Verify if <a href="../SmartSearch.html">SmartSearch</a> already loaded for
this model</p>
          

          
          <div class="method-source-code" id="is_smart_search-3F-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 59</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">is_smart_search?</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">included_modules</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">InstanceMethods</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- is_smart_search-3F-source -->
          
        </div>

        

        
      </div><!-- is_smart_search-3F-method -->

    
      <div id="method-i-result_template_path" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">result_template_path</span><span
            class="method-args">()</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>defines where to look for a partial to load when displaying results for
this model</p>
          

          
          <div class="method-source-code" id="result_template_path-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 64</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">result_template_path</span>
  <span class="ruby-node">&quot;/search/results/#{self.name.split(&quot;::&quot;).last.underscore}&quot;</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- result_template_path-source -->
          
        </div>

        

        
      </div><!-- result_template_path-method -->

    
      <div id="method-i-set_search_index" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">set_search_index</span><span
            class="method-args">()</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>reload search_tags for entire table based on the attributes defined in
&#39;:on&#39; option passed to the &#39;<a
href="ClassMethods.html#method-i-smart_search">#smart_search</a>&#39;
method</p>
          

          
          <div class="method-source-code" id="set_search_index-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 126</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">set_search_index</span>
  <span class="ruby-identifier">s</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">all</span>.<span class="ruby-identifier">size</span>.<span class="ruby-identifier">to_f</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">all</span>.<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">a</span>.<span class="ruby-identifier">create_search_tags</span> <span class="ruby-keyword">rescue</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">a</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:update_without_callbacks</span>)
    <span class="ruby-identifier">done</span> = ((<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>).<span class="ruby-identifier">to_f</span><span class="ruby-operator">/</span><span class="ruby-identifier">s</span>)*<span class="ruby-value">100</span>
    <span class="ruby-identifier">printf</span> <span class="ruby-node">&quot;Set search index for #{self.name}: #{done}%%                  \r&quot;</span>
  <span class="ruby-keyword">end</span>  
<span class="ruby-keyword">end</span></pre>
          </div><!-- set_search_index-source -->
          
        </div>

        

        
      </div><!-- set_search_index-method -->

    
      <div id="method-i-set_similarity_index" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">set_similarity_index</span><span
            class="method-args">()</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Load all search tags for this table into similarity index</p>
          

          
          <div class="method-source-code" id="set_similarity_index-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 137</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">set_similarity_index</span>
  
  <span class="ruby-identifier">search_tags_list</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">select_all</span>(<span class="ruby-node">&quot;SELECT search_tags from #{SmartSearchTag.table_name} where `table_name` = #{self.table_name}&quot;</span>).<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span> <span class="ruby-identifier">r</span>[<span class="ruby-string">&quot;search_tags&quot;</span>]}
  
  <span class="ruby-constant">SmartSimilarity</span>.<span class="ruby-identifier">create_from_text</span>(<span class="ruby-identifier">search_tags_list</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot; &quot;</span>))
<span class="ruby-keyword">end</span></pre>
          </div><!-- set_similarity_index-source -->
          
        </div>

        

        
      </div><!-- set_similarity_index-method -->

    
      <div id="method-i-smart_search" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">smart_search</span><span
            class="method-args">(options = {:on => [], :conditions => nil, :group => nil, :order => "created_at", :force => false})</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Enable <a href="../SmartSearch.html">SmartSearch</a> for the current
ActiveRecord model. accepts options:</p>
<ul><li>
<p>:on, define which attributes to add to the search index</p>
</li><li>
<p>:conditions, define default scope for all queries made</p>
</li><li>
<p>:group, group by column</p>
</li><li>
<p>:order, order by column</p>
</li></ul>

<p>see readme for details</p>
          

          
          <div class="method-source-code" id="smart_search-source">
            <pre><span class="ruby-comment"># File lib/smart_search.rb, line 27</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">smart_search</span>(<span class="ruby-identifier">options</span> = {<span class="ruby-value">:on</span> =<span class="ruby-operator">&gt;</span> [], <span class="ruby-value">:conditions</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">:group</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">:order</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;created_at&quot;</span>, <span class="ruby-value">:force</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">false</span>})
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">table_exists?</span>
    <span class="ruby-comment"># Check if search_tags exists</span>
    <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">is_smart_search?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:force</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">true</span>
      
      <span class="ruby-identifier">cattr_accessor</span> <span class="ruby-value">:condition_default</span>, <span class="ruby-value">:group_default</span>, <span class="ruby-value">:tags</span>, <span class="ruby-value">:order_default</span>, <span class="ruby-value">:enable_similarity</span>, <span class="ruby-value">:default_template_path</span>
      <span class="ruby-identifier">send</span> <span class="ruby-value">:include</span>, <span class="ruby-constant">InstanceMethods</span>
        <span class="ruby-keyword">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:after_save</span>, <span class="ruby-value">:create_search_tags</span>)
        <span class="ruby-keyword">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:before_destroy</span>, <span class="ruby-value">:create_search_tags</span>)
        <span class="ruby-keyword">self</span>.<span class="ruby-identifier">enable_similarity</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">true</span>
        
        <span class="ruby-comment"># options zuweisen</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>].<span class="ruby-identifier">blank?</span>
          <span class="ruby-keyword">self</span>.<span class="ruby-identifier">condition_default</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>]
        <span class="ruby-keyword">elsif</span> <span class="ruby-operator">!</span><span class="ruby-identifier">options</span>[<span class="ruby-value">:conditions</span>].<span class="ruby-identifier">nil?</span>
          <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;:conditions must be a valid SQL Query&quot;</span>  
        <span class="ruby-keyword">else</span>
          <span class="ruby-keyword">self</span>.<span class="ruby-identifier">condition_default</span> = <span class="ruby-keyword">nil</span>
        <span class="ruby-keyword">end</span>  
      
        <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">column_names</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&quot;created_at&quot;</span>)
          <span class="ruby-keyword">self</span>.<span class="ruby-identifier">order_default</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:order</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;created_at&quot;</span>
        <span class="ruby-keyword">else</span>  
          <span class="ruby-keyword">self</span>.<span class="ruby-identifier">order_default</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:order</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;id&quot;</span>
        <span class="ruby-keyword">end</span>  

        <span class="ruby-keyword">self</span>.<span class="ruby-identifier">tags</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:on</span>] <span class="ruby-operator">||</span> []
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>  
<span class="ruby-keyword">end</span></pre>
          </div><!-- smart_search-source -->
          
        </div>

        

        
      </div><!-- smart_search-method -->

    
    </section><!-- public-instance-method-details -->
  
  </section><!-- 5Buntitled-5D -->

</div><!-- documentation -->


<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.12.2.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

